/**
 * FBCMS Base JS Library 1.0
 *
 * @author qiuxj
 */
FBCMS = {
    // 项目路径前缀
    projectUrl: ctx,
    LIBS: 'dwr,jquery,commons,dialog',
    // private dwr
    dwrLib: function(){
        if (!window.dwr) {
            FBCMS.loadJs("engine,util", FBCMS.ctxPath + "dwr/");
        }
    },
    // private jquery
    jqueryLib: function(){
        if (!window.jquery) {
            FBCMS.loadJs("jquery-1.3.2", FBCMS.jsPath + "util/");
        }
    },
	dialogLib: function(){
        if (!window.jquery) {
            FBCMS.loadJs("jquery-ui,jquery.myext", FBCMS.jsPath + "util/");
        }
    },
    // private commons
    commonsLib: function(){
        if (!window.jquery) {
            FBCMS.loadJs("commons,se_hilite", FBCMS.jsPath + "util/");
        }
    },
    // private
    include: function(file){
        document.writeln('<script type="text/javascript" src="' + file + '?' + FBCMS.version + '"></script>');
    },
    // private 
    loadLib: function(libs){
        this.LIBS = libs ? libs : this.LIBS;
        var libs = this.LIBS.split(',');
        for (var i = 0; i < libs.length; i++) {
            try {
                FBCMS[libs[i] + 'Lib']();
            } catch (e) {
                alert('不支持的JS LIB:' + libs[i]);
            }
        }
    },
    // private
    load: function(src){
        var dwrs = src.match(/\?.*dwr=([A-Za-z0-9,\/]*)/);
        if (dwrs) {
            FBCMS.loadDwr(dwrs[1]);
        }
        var libs = src.match(/\?.*lib=([A-Za-z0-9,]*)/);
        FBCMS.loadLib(libs ? libs[1] : null);
        
        //FBCMS.loadJs('FBCMS-extend');
        
        var selfs = src.match(/\?.*self=([A-Za-z0-9,\-\/]*)/);
        FBCMS.loadSelf(selfs ? selfs[1] : null);
    },
    /**
     * 初始化需要加载js的文件
     */
    init: function(){
        var src, xmlHttp;
        var scripts = document.getElementsByTagName("script");
        FBCMS.ctxPath = FBCMS.projectUrl + '/';
        FBCMS.jsPath = FBCMS.ctxPath + 'scripts/';
        for (var i = 0; i < scripts.length; i++) {
            if (scripts[i].src && scripts[i].src.match(/fbcms-base\.js(\?.*)?$/)) {
                src = scripts[i].src;
            }
        }
        if (top.FBCMS.version) {
            FBCMS.version = top.FBCMS.version;
            FBCMS.load(src);
        } else {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                xmlHttp = new XMLHttpRequest();
            }
            if (xmlHttp) {
                xmlHttp.open("GET", FBCMS.ctxPath + "version.txt", false);
                xmlHttp.send(null);
                FBCMS.version = xmlHttp.responseText;
                FBCMS.load(src);
            } else {
                var myDate = new Date();
                FBCMS.version = "" + myDate.getYear() + myDate.getMonth() + myDate.getDate();
                FBCMS.load(src);
            }
        }
    },
    /**
     * 加载当前页面目录下的js文件
     */
    loadSelf: function(self){
        var location_path = window.location.pathname;
        if (!self || 'true' == self) {
            var fileName = location_path.substring(0, location_path.lastIndexOf(".htm")) + ".js";
            FBCMS.include(fileName);
        } else if (self && 'false' !== self) {
            var currPath = location_path.replace(/([A-Za-z0-9,]*)\.html(\?.*)?$/, '');
            FBCMS.loadJs(self, currPath);
        }
    },
    /**
     * 加载dwr远程service
     */
    loadDwr: function(dwrs){
        FBCMS.loadJs(dwrs, FBCMS.ctxPath + "dwr/interface/");
    },
    /**
     * 加载js文件
     */
    loadJs: function(jss, path){
        var jss = (jss ? jss : "").split(",");
        var path = path || FBCMS.jsPath;
        for (var i = 0; i < jss.length; i++) {
            FBCMS.include(path + jss[i] + ".js");
        }
    }
};

FBCMS.init();